package net.begincode.blog.controller;

import net.begincode.bean.BlogClassInfo;
import net.begincode.bean.BlogDetaiInfo;
import net.begincode.biz.BlogBiz;
import net.begincode.biz.HomeTypeBiz;
import net.begincode.biz.KeyWordsBiz;
import net.begincode.blog.bean.param.BlogClassQueryParam;
import net.begincode.blog.bean.vo.BlogBaseInfo;
import net.begincode.blog.entity.HomeType;
import net.begincode.blog.entity.KeyWords;
import net.begincode.blog.enums.BlogFlagEnum;
import net.begincode.common.ApiResponse;
import net.begincode.page.PageResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("blog/")
public class BlogController {

    @Resource
    private BlogBiz blogBiz;
    @Resource
    private HomeTypeBiz homeTypeBiz;
    @Resource
    private KeyWordsBiz keyWordsBiz;

    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    private ModelAndView view(HttpServletRequest request, @PathVariable("id") Long blogId) {
        ModelAndView modelAndView = new ModelAndView();
        BlogDetaiInfo blogDetaiInfo = blogBiz.queryBlogById(blogId, BlogFlagEnum.PUBLIC.getFlagCode());
        modelAndView.addObject("blog", blogDetaiInfo);
        modelAndView.setViewName("detail");
        return modelAndView;
    }

    @RequestMapping(value = "/type/{id}/{page}", method = RequestMethod.GET)
    private ModelAndView type(@PathVariable("id") Long typeId) {
        ModelAndView modelAndView = new ModelAndView();
        BlogClassQueryParam queryParam = new BlogClassQueryParam();
        queryParam.setBlogTypeId(typeId);
        PageResponse<BlogClassInfo> classifyBlogs = blogBiz.queryClassByQuery(queryParam);
        HomeType homeType = homeTypeBiz.queryById(typeId);
        if (Objects.nonNull(homeType)) {
            modelAndView.addObject("classifyName", homeType.getTypeName());
            modelAndView.addObject("classifyId", homeType.getId());
        }
        modelAndView.addObject("classify", classifyBlogs);
        modelAndView.setViewName("classify");
        return modelAndView;
    }

    @RequestMapping(value = "/keyword/{id}/{page}", method = RequestMethod.GET)
    private ModelAndView keyword(@PathVariable("id") Long keywordId) {
        ModelAndView modelAndView = new ModelAndView();
        BlogClassQueryParam queryParam = new BlogClassQueryParam();
        queryParam.setKeyWordId(keywordId);
        PageResponse<BlogClassInfo> classifyBlogs = blogBiz.queryClassByQuery(queryParam);
        KeyWords keyWords = keyWordsBiz.queryById(keywordId);
        if (Objects.nonNull(keyWords)) {
            modelAndView.addObject("classifyName", keyWords.getKeyWord());
            modelAndView.addObject("classifyId", keyWords.getId());
        }
        modelAndView.addObject("classify", classifyBlogs);
        modelAndView.setViewName("keyword");
        return modelAndView;
    }


    @RequestMapping(value = "/blogTop", method = RequestMethod.POST)
    @ResponseBody
    public ApiResponse<List<BlogBaseInfo>> queryRecommend(){
        List<BlogBaseInfo> blogBaseInfos =  blogBiz.selectTopN(BlogFlagEnum.PUBLIC.getFlagCode(),10);
        if(CollectionUtils.isEmpty(blogBaseInfos)){
            return ApiResponse.fail("没有推荐信息");
        }
        return ApiResponse.success(blogBaseInfos);
    }

}
